package com.ruoyi.record.service.impl;

import java.util.List;

import com.ruoyi.record.common.RecordConstant;
import com.ruoyi.record.domain.WordStudyStep;
import com.ruoyi.record.service.IWordStudyStepService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import com.ruoyi.record.mapper.WordStudyStepDetailMapper;
import com.ruoyi.record.domain.WordStudyStepDetail;
import com.ruoyi.record.service.IWordStudyStepDetailService;

/**
 * 单词学习任务详情Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-03
 */
@Service
public class WordStudyStepDetailServiceImpl implements IWordStudyStepDetailService 
{
    @Autowired
    private WordStudyStepDetailMapper wordStudyStepDetailMapper;

    private IWordStudyStepService wordStudyStepService;

    public WordStudyStepDetailServiceImpl(@Lazy IWordStudyStepService wordStudyStepService) {
        this.wordStudyStepService = wordStudyStepService;
    }

    /**
     * 查询单词学习任务详情
     * 
     * @param id 单词学习任务详情主键
     * @return 单词学习任务详情
     */
    @Override
    public WordStudyStepDetail selectWordStudyStepDetailById(Long id)
    {
        return wordStudyStepDetailMapper.selectWordStudyStepDetailById(id);
    }

    /**
     * 查询单词学习任务详情列表
     * 
     * @param wordStudyStepDetail 单词学习任务详情
     * @return 单词学习任务详情
     */
    @Override
    public List<WordStudyStepDetail> selectWordStudyStepDetailList(WordStudyStepDetail wordStudyStepDetail)
    {
        return wordStudyStepDetailMapper.selectWordStudyStepDetailList(wordStudyStepDetail);
    }

    /**
     * 新增单词学习任务详情
     * 
     * @param wordStudyStepDetail 单词学习任务详情
     * @return 结果
     */
    @Override
    public int insertWordStudyStepDetail(WordStudyStepDetail wordStudyStepDetail)
    {
        wordStudyStepDetail.setActive(RecordConstant.ACTIVE_READY);
        wordStudyStepDetail.setStatus(RecordConstant.STATUS_NORMAL);
        int inserted = wordStudyStepDetailMapper.insertWordStudyStepDetail(wordStudyStepDetail);
        //更新主表
        Integer main = wordStudyStepDetail.getMain();
        updateMainTable(main);
        return inserted;
    }

    public void updateMainTable(Integer id){
        WordStudyStep studyStep = new WordStudyStep();
        studyStep.setId(Long.valueOf(id));
        WordStudyStepDetail stepDetail = new WordStudyStepDetail();
        stepDetail.setMain(id);
        List<WordStudyStepDetail> detailList = wordStudyStepDetailMapper.selectWordStudyStepDetailList(stepDetail);
        studyStep.setTotal(detailList.size());
        wordStudyStepService.updateWordStudyStep(studyStep);
    }

    /**
     * 修改单词学习任务详情
     * 
     * @param wordStudyStepDetail 单词学习任务详情
     * @return 结果
     */
    @Override
    public int updateStepDetailByMain(WordStudyStepDetail wordStudyStepDetail)
    {
        return wordStudyStepDetailMapper.updateStepDetailByMain(wordStudyStepDetail);
    }

    @Override
    public int updateWordStudyStepDetail(WordStudyStepDetail wordStudyStepDetail)
    {
        return wordStudyStepDetailMapper.updateWordStudyStepDetail(wordStudyStepDetail);
    }



    /**
     * 批量删除单词学习任务详情
     * 
     * @param ids 需要删除的单词学习任务详情主键
     * @return 结果
     */
    @Override
    public int deleteWordStudyStepDetailByIds(Long[] ids)
    {
        return wordStudyStepDetailMapper.deleteWordStudyStepDetailByIds(ids);
    }

    /**
     * 删除单词学习任务详情信息
     * 
     * @param id 单词学习任务详情主键
     * @return 结果
     */
    @Override
    public int deleteWordStudyStepDetailById(Long id)
    {
        return wordStudyStepDetailMapper.deleteWordStudyStepDetailById(id);
    }
}
